package com.promotores;

import java.sql.Date;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AdapterPromocion extends BaseAdapter{
	
	List<BeanPromocion> lista;
	Context context;
	LayoutInflater inflater;
	
	public AdapterPromocion(List<BeanPromocion> lista, Context context){
		this.lista = lista;
		this.context = context;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return lista.size();
	}

	@Override
	public Object getItem(int position) {
		return lista.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		
		if (convertView == null){
			convertView = inflater.inflate(R.layout.pr_item_ev_promocion, null);
			
			holder = new ViewHolder();
			holder.dias = (TextView) convertView.findViewById(R.id.datDias);
			holder.tipoPromocion = (TextView) convertView.findViewById(R.id.datTipoPromocion);
			holder.fecIni = (TextView) convertView.findViewById(R.id.datFecIni);
			holder.fecFin = (TextView) convertView.findViewById(R.id.datFecFin);
			holder.descuento = (TextView) convertView.findViewById(R.id.datDescuento);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder)convertView.getTag();
		}
		
		BeanPromocion promocion = lista.get(position);
		String strDias="";
		for (int i=0; i<promocion.getDias().length(); i++){
			if (promocion.getDias().charAt(i)=='1')
				switch (i) {
				case 0 : { strDias+="Lun "; break; }
				case 1 : { strDias+="Mar "; break; }
				case 2 : { strDias+="Mie "; break; }
				case 3 : { strDias+="Jue "; break; }
				case 4 : { strDias+="Vie "; break; }
				case 5 : { strDias+="Sab "; break; }
				case 6 : { strDias+="Dom"; break; }
				}
		}
		holder.dias.setText(strDias);
		holder.tipoPromocion.setText(promocion.getTipoPromocion().getTipoPromocion());
		holder.fecIni.setText(promocion.getFecIni().toString());
		holder.fecFin.setText(promocion.getFecFin().toString());
		holder.descuento.setText(String.format("%3d",(int)Math.round(promocion.getDescuento()))+"% ");
		return convertView;
	}
	
	class ViewHolder{
		TextView tipoPromocion;
		TextView fecIni;
		TextView fecFin;
		TextView dias;
		TextView descuento;
	}
}